package stephen.com.controller.view;

import stephen.com.controller.BaseServlet;
import stephen.com.service.UserService;
import stephen.com.service.impl.UserServiceImpl;
import stephen.com.utils.ContainChineseUtil;
import stephen.com.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {
	private UserService userService = new UserServiceImpl();

	public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		System.out.println("来到index页面！");
		req.getRequestDispatcher("/index.jsp").forward(req, resp);
	}

	public void home(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("来到home页面！");
		//跳主页jsp，顺便去后台查询用户的基本信息
		Object obj = req.getSession().getAttribute("userId");
		//		System.out.println("userId: "+obj);
		if ( null != obj ) {
			Integer userId = Integer.parseInt(obj.toString());
			Map<String, Object> map = userService.findUserBaseById(userId);
			req.setAttribute("userInfoById", map);
		}
		req.getRequestDispatcher("/html/home.jsp").forward(req, resp);
	}

	public void article(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		System.out.println("来到article页面！");
		req.getRequestDispatcher("/html/article.jsp").forward(req, resp);
	}

	public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		System.out.println("来到register页面！");
		req.getRequestDispatcher("/register.jsp").forward(req, resp);
	}

	public void forget(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		System.out.println("来到forget页面！");
		req.getRequestDispatcher("/forget.jsp").forward(req, resp);
	}

	public void user(HttpServletRequest req,HttpServletResponse resp){
		try {
			//获取前端提交的数据currentPage
			String currentPage = req.getParameter("page");
			String search = req.getParameter("search");
			if(null != search && !ContainChineseUtil.isContainChinese(search)){
				search = URLDecoder.decode(search,"UTF-8");
				byte[] bytes = search.getBytes("ISO-8859-1");
				search = new String(bytes,"UTF-8");
			}
			//如果前端未提交数据，则默认为1
			int page = 1;
			if(null != currentPage){
				//字符串转为int
				page = Integer.parseInt(currentPage);
			}

			String userId = req.getSession().getAttribute("userId").toString();
			//通过当前页数获取查询到的用户列表相应分组的数据
			List<Map<String, Object>> userList = userService.findList(Integer.parseInt(userId), page, search);
			req.setAttribute("userList", userList);

			//获取总页数
			long sumPage = PageUtil.getSumPage(userService.sumUsers());
//			long totalPage = PageUtil.getSumPage(userService.sumUsers(search));
			long startPage = PageUtil.getStartPage(page) + 1;
			long endPage = PageUtil.getEndPage(page, sumPage);
			req.setAttribute("sumPage", sumPage);
			req.setAttribute("currentPage",page);
			req.setAttribute("startPage",startPage);
			req.setAttribute("endPage",endPage);
//			req.setAttribute("search", search);

			//再去后台查询用户的基本信息
//			Object obj = req.getSession().getAttribute("userId");
//			if (null != obj) {
//				Integer uId = Integer.parseInt(obj.toString());
//				//根据id查询用户信息
//				Map<String, Object> map = userService.findUserBaseById(uId);
//				req.setAttribute("userBaseInfo", map);
//			}
			req.getRequestDispatcher("/html/user.jsp").forward(req,resp);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
